#####
#####
###     This R session creates Figures 3 and 4 in 
###     "A New Measure of Policy Spending Priorities 
###     in the American States
#####
#####

library(lattice)

###
###   Read ideal points data from file 
###   "Raw state points, with names.txt".
###

states1 <- read.table(file.choose(), header = T)

###
###     Transform ideal points so differences
###     equal differences in spending on CGs,
###     and look at distribution of state points
###

states1$tran.pts <- -1.62519  + (states1$state.point * 4.50)

summary(states1$tran.pts)

states1$final.pts <- states1$tran.pts - mean(states1$tran.pts)

summary(states1$final.pts)

###
###   The following function creates a full-color 
###   version of Figure 3 from the PA article
###

histogram(~final.pts, data = states1,
   aspect = .75, 
   xlab = "Unfolded state policy priority scores")


### 
###     The following creates a non-color version of Figure 3,
###     as per POLITICAL ANALYSIS instructions
###

plot.poly.settings <- trellis.par.get("plot.polygon")

plot.poly.settings

plot.poly.settings$col <- "gray"

trellis.par.set("plot.polygon", plot.poly.settings)

histogram(~final.pts, data = states1,
   aspect = .75, 
   xlab = "Unfolded state policy priority scores")

###
###   The following functions customize the appearance
###   of the lattice boxplot, and create Figure 4 
###   from the PA article
###

box.rectangle <- trellis.par.get("box.rectangle")

box.rectangle$col <- "black"

trellis.par.set("box.rectangle", box.rectangle)

box.umbrella <- trellis.par.get("box.umbrella")

box.umbrella$lty <- 1

box.umbrella$col <- "black"

trellis.par.set("box.umbrella", box.umbrella)

plot.symbol <- trellis.par.get("plot.symbol")

plot.symbol$col <- "black"

trellis.par.set("plot.symbol", plot.symbol)

###
###   The following function creates Figure 4
###

bwplot(as.factor(year) ~ final.pts, data = states1,
   aspect = 1, 
   xlab = "Unfolded state policy priority scores",
   ylab = "Year",
   pch = "|",
   scales = list(cex = .75),
   par.settings = list(box.rectangle = list(col = "black"),
     box.umbrella = list(col = "black", lty = "solid"),
     plot.symbol = list(col = "black"))
)

